<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 4.1.&nbsp; Expressions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-4-SECT-1"></a>
<h3 id="631368-828" class="docSection1Title">4.1. Expressions</h3>
<a name="IDX-CHP-4-0107"></a> 
<a name="IDX-CHP-4-0108"></a> 
<a name="IDX-CHP-4-0109"></a> 
<a name="IDX-CHP-4-0110"></a> 

<p class="docText">There are several building blocks of coding that you need to understand: statements, expressions, and operators.<a name="IDX-CHP-4-0111"></a> 
 A <span class="docEmphasis">statement</span> is code that performs a task. Statements themselves are made up of expressions and operators. An <span class="docEmphasis">expression</span> is a piece of code that evaluates to a value. A value is a number, a string of text, or a <span class="docEmphasis">Boolean</span>.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><td valign="top">
<p class="docText">A Boolean is an expression that results in a value of either <tt>trUE</tt> or <tt>FALSE</tt>. For example, the expression 10 &gt; 5 (10 is greater than 5) is a Boolean expression because the result is <tt>trUE</tt>. All expressions that contain <span class="docEmphasis">relational operators</span>, such as the less-than sign (<tt>&lt;</tt>), are Boolean. The Boolean operators are <tt>AND, OR, XOR, NOR</tt>, and <tt>NOT</tt>.</P>
</td></tr></table></TD></TR></table></p><BR>
<p class="docText">An <span class="docEmphasis">operator</span> is a code element that acts on an expression in some way. For instance, a minus sign can be used to tell the computer to decrement the value of the expression after it from the expression before it. The most important thing to understand about expressions is how to combine them into compound expressions and statements using operators. So we're going to look at operators used to turn expressions into more complex expressions and statements.</p>
<p class="docText">The simplest form of expression is a literal or a variable. A <span class="docEmphasis">literal</span> evaluates to itself. Some examples of literals<a name="IDX-CHP-4-0112"></a> 
<a name="IDX-CHP-4-0113"></a> 
 are numbers, strings, or constants. A <span class="docEmphasis">variable</span> evaluates to the value assigned to it. For instance, any of the expressions in <a class="docLink" href="#learnphpmysql-CHP-4-TABLE-1">Table 4-1</a> are valid.</p>
<a name="learnphpmysql-CHP-4-TABLE-1"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><H5 class="docTableTitle">Table 4-1. Valid expressions</h5></caption><colgroup span="2"><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Example</P></th><th class="thead" scope="col" align="left"><p class="docText">Type</p></th></tr></thead><TR><td class="docTableCell" align="left"><p class="docText">1</p></td><td class="docTableCell" align="left"><p class="docText">A numeric value literal</P></TD></tr><tr><td class="docTableCell" align="left"><p class="docText">"Becker Furniture"</p></TD><td class="docTableCell" align="left"><p class="docText">A string literal</P></td></TR><TR><TD class="docTableCell" align="left"><p class="docText">TRUE</p></td><td class="docTableCell" align="left"><p class="docText">A constant literal</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">$user_name</p></td><td class="docTableCell" align="left"><p class="docText">A variable with username as a string</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">1+1</p></TD><TD class="docTableCell" align="left"><p class="docText">A numeric value expression that evaluates to a literal</p></TD></tr></table></p><br>
<p class="docText">Although a literal or variable may be a valid expression, they aren't expressions that do anything. You get expressions to do things such as math or assignment by linking them together with operators.<a name="IDX-CHP-4-0114"></a> 
</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><td valign="top">
<p class="docText">Assignment is a symbol that represents a specific action. For example, a plus sign (+) is an operator that represents addition.</p>
</td></tr></table></TD></TR></table></p><br>
<p class="docText">An <span class="docEmphasis">operator</span> combines simple expressions into more complex expressions by creating relationships between simple expressions that can be evaluated. For instance, if the relation you want to establish is the cumulative joining of two numeric values together, you could write "3 + 4".</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-4-FIG-1">Figure 4-1</a> shows how the parts of an expression come together.</p>
<a name="learnphpmysql-CHP-4-FIG-1"></a><P><center>
<h5 class="docFigureTitle">Figure 4-1. Operands and operators working together as an expression to form a value</H5>
<img border="0" alt="" id="195131084199" width="438" height="173" SRC="images/learnphpmysql_0401.jpg">
</center></p><BR>
<p class="docText">The numbers 3 and 4 are each valid expressions. The equation 3 + 4 is also a valid expression, whose value, in this case, happens to be 7. The plus sign (<tt>+</tt>) is an operator. The numbers to either side of it are its arguments,<a name="IDX-CHP-4-0115"></a> 
 or operands.<a name="IDX-CHP-4-0116"></a> 
 An <span class="docEmphasis">argument</span> or <span class="docEmphasis">operand</span> is something an operator takes action on; for example, an argument or operand could be a directive from your housemate to empty the dishwasher, and the operator empties the dishwasher. Different operators have different types and numbers of operands. Operators can also be <span class="docEmphasis">overloaded</span>, which means that they do different things in different contexts.</P>
<p class="docText">You've probably guessed from this information that two or more expressions connected by operators are called an expression. You're right, as operators create complex expressions. The more subexpressions and operators, the longer and more complex the expression. But no matter what, as long as it equates to a value, it's still an expression.</P>
<p class="docText">When expressions and operators are assembled to produce a piece of code that actually does something, you have a statement. We discussed statements in <a class="docLink" href="learnphpmysql-CHP-3.html#learnphpmysql-CHP-3">Chapter 3</a>. They end in semicolons, which is the programming equivalent of a complete sentence.</p>
<p class="docText">For instance, <tt>$Margaritaville + $Sun_Tan_Application</tt> is an expression. It equates to something, which is the sum of the values of <tt>$Margaritaville + $Sun_Tan_Application</tt>, but it doesn't do anything. While it's an expression, the output doesn't make any sense, but if you add the equals sign (<tt>=</tt>), <tt>$Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application;</tt>, you get a statement, because it does something. As <a class="docLink" href="#learnphpmysql-CHP-4-EX-1">Example 4-1</a> demonstrates, it assigns the sum of the values of <tt>$Margaritaville + $Sun_Tan_Application</tt> to <tt>$Fun_in_the_Sun</tt>.</p>
<a name="learnphpmysql-CHP-4-EX-1"></a><h5 id="title-IDAJYKME" class="docExampleTitle">Example 4-1. Sum of values</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$Margaritaville = 3; // Three margaritas
$Sun_Tan_Application = 2; // Two applications of sun tan
$Fun_in_the_Sun = $Margaritaville + $Sun_Tan_Application;
echo $Fun_in_the_Sun;
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-4-EX-1">Example 4-1</a> outputs:</p>
<pre>
5
</pre><br>

<p class="docText">There really isn't much more to understand about expressions except for the assembly of them into compound expressions and statements using operators. Next, we're going to discuss operators that are used to turn expressions into more complex expressions and statements.</p>

</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-4-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
